<%@page import="com.cinemtl.beans.Movie"%>
<%@page import="com.cinemtl.manager.MovieManager"%>
<%@page import="com.cinemtl.beans.CartOrder"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Enumeration"%>
<%@page import="com.cinemtl.beans.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
	
<%
	if(session.getAttribute("user") != null){
		User user = (User) session.getAttribute("user");
		if(user.getUsername().equals("admin")){
	request.getRequestDispatcher("admin").forward(request,response);
		}
	}
%>

<!DOCTYPE html>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Cine-MTL - Cart</title>
<link href='http://fonts.googleapis.com/css?family=Open+Sans'
	rel='stylesheet' type='text/css'>
<link rel="stylesheet"
	href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/custom.css" />
<link rel="stylesheet" href="css/home.css" type="text/css" />

<link rel="stylesheet" href="css/flat-ui.min.css" type="text/css" />
</head>

<body>



	<div class="container">

		<div class="row">
			<header>
				<a id="logo" href="home.jsp"><img src="images/cine-mtl-logo.png"></img></a>
				<div id="welcome"
					class="col-xs-offset-3 col-sm-offset-4 col-md-offset-6 col-lg-offset-7">
					<%
						User user = null;
									if (session.getAttribute("user") != null) {
										 user = (User) session.getAttribute("user");
					%>

					<div id="showUsername">
						<span id="nameUser">Welcome <%=user.getUsername()%>
						</span><span class="fui-exit" id="logout"></span>
					</div>

					<%
						} else {
					%>
					<div id="showUsername">
						<span id="nameUser">Welcome Guest</span>
					</div>

					<%
						}
					%>

				</div>
				<nav class="navbar navbar-default navbar-embossed" role="navigation">
					<div class="navbar-header">
						<button type="button" class="navbar-toggle" data-toggle="collapse"
							data-target="#bs-example-navbar-collapse-1">
							<span class="sr-only">Toggle navigation</span> <span
								class="icon-bar"></span> <span class="icon-bar"></span> <span
								class="icon-bar"></span>
						</button>
					</div>
					<div class="collapse navbar-collapse"
						id="bs-example-navbar-collapse-1">
						<ul class="nav navbar-nav">
							<li><a href="home.jsp">Home</a></li>

							<li><a href="movies.jsp">Movies</a></li>
							<li><a href="contact.jsp">Contact</a></li>
						</ul>
						<form class="navbar-form navbar-right" action="search"
							method="get" role="search">
							<div class="form-group">
								<div class="input-group">
									<input class="form-control" id="navbarInput-01" type="search"
										name="search" placeholder="Search"> <span
										class="input-group-btn">
										<button type="submit" class="btn">
											<span class="fui-search"></span>
										</button>
									</span>
								</div>
							</div>
						</form>
						<%
							if (session.getAttribute("user") == null) {
						%>
						<div id="inout">
							<ul class="nav navbar-nav navbar-right">
								<li><a href="#" data-toggle="modal"
									data-target="#loginModal" class="">Log in</a></li>
								<li><a href="register.html">Sign up</a></li>
							</ul>
						</div>
						<%
							}
						%>
					</div>

				</nav>
			</header>

			<div class="container text-center">
				<h3>Your online cart</h3>
				<div id="notify-success" class="alert alert-success" role="alert">You
					order has been successfully processed</div>
				<div class="col-md-7 col-lg-15 col-sm-12">
					<ul>
						<li class="row list-inline columnCaptions"><span>QTY</span> <span>ITEM</span>
							<span>Price</span></li>

						<%
							Enumeration<String> keys=session.getAttributeNames();
										HashMap<Integer,CartOrder> orders = new HashMap<Integer,CartOrder>();
										while(keys.hasMoreElements())
										{
											if (keys.nextElement().equals("cart")) {
												orders = (HashMap<Integer, CartOrder>) session
														.getAttribute("cart");
											}
										}
									
										if (!orders.isEmpty()) {
									
										for(CartOrder order: orders.values())
										{
											Movie movie = MovieManager.getById(order.getId());
						%>

						<li id="<%=movie.getId()%>" class="row cartItem"><input
							type="number" class="quantity" data-movie="<%=movie.getId()%>"
							value="<%=order.getNbTicket()%>" min="0"> <span
							class="itemName"><%=movie.getTitle()%></span> <span
							class="fui-cross popbtn" data-remove="<%=movie.getId()%>"></span>
							<span class="price"><%=order.getNbTicket()*9.98%></span></li>

						<%
							}
															}
						%>
					</ul>
				</div>
				<%
					double total = 0;
										int widgets=0;
										for(CartOrder order: orders.values())
										{
											total+=order.getNbTicket()*9.98;
											widgets+=order.getNbTicket();
										}
				%>
				<%
					if (session.getAttribute("user") != null && !orders.isEmpty()) {
				%>
				<a class="btn btn-success" id="checkout"
					data-user="<%=user.getId()%>">checkout</a>
				<%
					}else if(session.getAttribute("user") == null){
				%>
				<a class="btn btn-danger" data-toggle="modal"
					data-target="#loginModal" id="loginCart">Login</a>
				<%
					}
				%>
			</div>
			<footer>
				<div class="row">
					<div class="col-lg-12">
						<h5>Copyright &copy; Cine-MTL 2015</h5>
					</div>
				</div>
			</footer>
		</div>
	</div>
	<div class="modal fade" id="loginModal" tabindex="-1" role="dialog"
		aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-hidden="true">&times;</button>
					<h4 class="modal-title">Log in</h4>
				</div>

				<div class="modal-body">
					<form id="loginForm" method="post" class="form-horizontal"
						action="">
						<div class="login-form">
							<div class="form-group">
								<input type="text" class="form-control login-field"
									name="username" placeholder="Username" id="login-name" required />
								<label class="login-field-icon fui-user" for="login-name"></label>
							</div>

							<div class="form-group">
								<input type="password" class="form-control login-field"
									name="password" placeholder="Password" id="login-pass" required />
								<label class="login-field-icon fui-lock" for="login-pass"></label>
							</div>
							<input class="btn btn-primary btn-lg btn-block" type="button"
								id="submit" value="login"> <a class="login-link"
								href="redirect">Lost your password?</a>

						</div>
					</form>
				</div>
			</div>

		</div>
	</div>

</body>
<script
	src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="js/flat-ui.min.js"></script>
<script src="js/customjs.js"></script>
<script src="js/cart.js"></script>
<script type="text/javascript" src="js/sha1.js"></script>
<script type="text/javascript" src="js/main.js"></script>
<script>
	$('.input-group').on('focus', '.form-control', function() {
		$(this).closest('.input-group, .form-group').addClass('focus');
	}).on('blur', '.form-control', function() {
		$(this).closest('.input-group, .form-group').removeClass('focus');
	});
</script>

</html>